<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Controllers
 * @see 		Profile
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Profile extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->load->model('hiti_model', 'hiti', true);
		$this->hiti->load_config();
		if (!$this->session->userdata('logged_in'))
		{
			redirect();
		}
		$this->load->model($GLOBALS['var']['act'].'_model', 'Prof');
		$this->uri_arr = array(
			'deleted' => $GLOBALS['var']['deleted'], 
			'q' => '',
			'rowstart' => ''
		);
	}

	public function index() {
		/*
		* Xu ly du lieu
		*/
		$id = $GLOBALS['var']['user_id'];
		$data = array(
			'updated' => $this->input->get('updated', true),
			'failed' => $this->input->get('failed', true),
			'token' => $this->session->userdata('token', true)
		);
		$data['user'] = array();
		$user = $this->Prof->user($id);		
		if($user) $data['user'] = $user;
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Thông tin cá nhân',
			'add_link' => '',
			'search' => false,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => 'Cập nhập',
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view('profile', $data);
		$this->load->view('footer');
	}

	public function update()
	{
		/*
		* Kiem tra POST method
		*/
		if (!$_POST)
		{
			redirect();
		}
		/*
		* Kiem tra token va tham so yeu cau
		*/
		$id = $this->input->post('id', true);
		if (!token_validation() || !$id)
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Xu ly du lieu
		*/
		$data = array(
			'email' => $this->input->post('email', true),
			'phone' => $this->input->post('phone', true)
		);
		$new_pass = $this->input->post('new_password', true);
	  	if($new_pass != '') $data['password'] = md5($new_pass);
	  	/*
		* Upload file
		*/
		$this->load->library('upload');
		$upload_data = array();
		if ($_FILES['userfile']['name'])
		{
			$upload_data = upload('', $GLOBALS['user']['username'], 'img/avatar/');
			if ($upload_data['file_name'])
			{
				make_thumb($upload_data['full_path'], $upload_data['file_path'].'thumbs/'.$upload_data['file_name'], 120, 120);
				$data['icon'] = $upload_data['file_name'];
			}
		}
	  	/*
		* Ghi du lieu va chuyen huong
		*/
		if ($this->Prof->update($data, $id))
		{
			redirect($GLOBALS['var']['act'].'?updated=1');
		}
		else
		{
			redirect($GLOBALS['var']['act'].'?failed=1');
   		}
	}
}

/* End of file profile.php */
/* Location: ./application/controllers/profile.php */
